﻿@using SimpleIdServer.IdServer.Api.Token.Handlers;
@using SimpleIdServer.IdServer.Helpers
@using SimpleIdServer.IdServer.Website.Infrastructures
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.AcrsStore;
@using SimpleIdServer.IdServer.Website.Stores.ScopeStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@using SimpleIdServer.IdServer.WsFederation
@using SimpleIdServer.IdServer.WsFederation.Extensions;
@using SimpleIdServer.IdServer.Saml.Idp.Extensions;
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject IDispatcher dispatcher
@inject IState<UpdateClientState> updateClientState
@inject IState<AcrsState> acrsState
@inject TooltipService tooltipService
@inject NotificationService notificationService

<RadzenTemplateForm Submit=@SubmitUpdateClient TItem="UpdateClient" Data=@updateClient>
    <RadzenAccordion Multiple=true>    
        <Items>
            <RadzenAccordionItem Text="@Global.GeneralSettings" Selected=true>
                <!-- Client ID -->
                <div class="row">
                    <div class="col-md-2">
                        <RadzenLabel Text="@Global.ClientIdentifier" Style="padding: 0px;" />
                    </div>
                    <div class="col">
                        <RadzenTextBox Name="ClientId" @bind-Value="@updateClient.ClientId" Class="w-100" Disabled=true></RadzenTextBox>
                    </div>
                </div>
                <!-- Name -->
                <div class="row mt-1">
                    <div class="col-md-2">
                        <RadzenLabel Text="@Global.Name" Style="padding: 0px;" />
                    </div>
                    <div class="col">
                        <RadzenTextBox Name="ClientName" @bind-Value="@updateClient.ClientName" Class="w-100"></RadzenTextBox>
                    </div>
                </div>
                @if(Client.ClientType == ClientTypes.MOBILE || Client.ClientType == ClientTypes.SPA)
                {
                    <div class="row mt-1">
                        <div class="col-md-2">
                            <RadzenIcon Icon="info" MouseEnter="@(args => ShowTooltip(args, Global.IsPublicTooltip))" />
                            <RadzenLabel Text="@Global.IsPublic" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenCheckBox Name="IsPublic" @bind-Value="@updateClient.IsPublic"></RadzenCheckBox>
                        </div>
                    </div>
                }
                <!-- Access Token Type -->
                @if(Client.ClientType != ClientTypes.WSFEDERATION && Client.ClientType != ClientTypes.SAML)
                {                    
                    <div class="row mt-1">
                        <div class="col-md-2">
                            <RadzenLabel Text="@Global.AccessTokenType" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenDropDown Name="AccessTokenType" 
                            class="w-100"
                            Data=@AllAccessTokenTypes
                            TextProperty="Description"
                            ValueProperty="Value"
                            TValue="AccessTokenTypes"
                            @bind-Value=@updateClient.AccessTokenType />
                        </div>
                    </div>
                }

                @if(Client.ClientType == ClientTypes.FEDERATION)
                {
                    <!-- Client registration types -->
                    <div class="row mt-1">
                        <div class="col-md-2">
                            <RadzenLabel Text="@Global.ClientRegistrationTypes" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenTextBox Name="ConcatenatedClientRegistrationTypes" @bind-Value="@updateClient.ConcatenatedClientRegistrationTypes" Class="w-100" Disabled=true></RadzenTextBox>
                        </div>
                    </div>
                    <!-- Expiration time -->
                    <div class="row mt-1">
                        <div class="col-md-2">
                            <RadzenLabel Text="@Global.ClientExpirationTime" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenDatePicker @bind-Value=@updateClient.ExpirationDateTime Class="w-100" Disabled=true />
                        </div>
                    </div>
                }
            </RadzenAccordionItem>
            @if (Client.ClientType == ClientTypes.CREDENTIALISSUER || 
               Client.ClientType == ClientTypes.SPA || 
               Client.ClientType == ClientTypes.WEBSITE || 
               Client.ClientType == ClientTypes.EXTERNAL || 
               Client.ClientType == ClientTypes.MOBILE || 
               Client.ClientType == ClientTypes.HIGHLYSECUREDWEBSITE ||
               Client.ClientType == ClientTypes.GRANTMANAGEMENT)
            {
                <RadzenAccordionItem Text="@Global.AccessSettings" Selected=true>
                    <!-- Redirect URIs -->
                    <div class="row">
                        <div class="col-md-2">
                            <RadzenIcon Icon="info" MouseEnter="@(args => ShowTooltip(args, Global.ValidationRedirectionUrisTooltip))" />
                            <RadzenLabel Text="@Global.ValidationRedirectionUris" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenTextBox Name="RedirectionUris" @bind-Value="@updateClient.RedirectionUrls" Class="w-100"></RadzenTextBox>
                        </div>
                    </div>
                    <!-- Post logout redirect URIs -->
                    <div class="row mt-2">
                        <div class="col-md-2">
                            <RadzenIcon Icon="info" MouseEnter="@(args => ShowTooltip(args, Global.ValidationPostLogoutRedirectUrisTooltip))" />
                            <RadzenLabel Text="@Global.ValidationPostLogoutRedirectUris" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenTextBox Name="PostLogoutRedirectUris" @bind-Value="@updateClient.PostLogoutRedirectUris" Class="w-100"></RadzenTextBox>
                        </div>
                    </div>
                    <!-- Redirect URL is case sensitive -->
                    <div class="row mat-2">
                        <div class="col-md-2">
                            <RadzenIcon Icon="info" MouseEnter="@(args => ShowTooltip(args, Global.RedirectUrlCaseSensitiveTooltip))" />
                            <RadzenLabel Text="@Global.RedirectUrlCaseSensitive" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenCheckBox Name="IsRedirectUrlCaseSensitive" @bind-Value="@updateClient.IsRedirectUrlCaseSensitive"></RadzenCheckBox>
                        </div>
                    </div>
                    <div class="row mat-2">
                        <div class="col-md-2">
                            <RadzenIcon Icon="info" MouseEnter="@(args => ShowTooltip(args, Global.ManualActionRequiredRevokeSessionTooltip))" />
                            <RadzenLabel Text="@Global.ManualActionRequiredRevokeSession" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenCheckBox Name="RedirectToRevokeSessionUI" @bind-Value="@updateClient.RedirectToRevokeSessionUI"></RadzenCheckBox>
                        </div>
                    </div>
                </RadzenAccordionItem>
            }

            @if(Client.ClientType != ClientTypes.WSFEDERATION && Client.ClientType != ClientTypes.FEDERATION)
            {
                <RadzenAccordionItem Text="@Global.CapabilityConfig" Selected=true>
                    <!-- Grant types -->
                    @if(Client.ClientType != ClientTypes.SAML)
                    {
                        <div class="row">
                            <div class="col-md-2">
                                <RadzenLabel Text="@Global.AccessRequest" Style="padding: 0px;" />
                            </div>
                            <div class="col">
                                @if (Client.ClientType == ClientTypes.MACHINE)
                                {
                                    <!-- Client credentials  -->
                                    <div>
                                        <RadzenCheckBox Name="IsClientCredentialsGrantTypeEnabled" Disabled=true @bind-Value=updateClient.IsClientCredentialsGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.ClientCredentials" Component="IsClientCredentialsGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                    <!-- Password -->
                                    <div>
                                        <RadzenCheckBox Name="IsPasswordGrantTypeEnabled" @bind-Value=updateClient.IsPasswordGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.Password" Component="IsPasswordGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                    <!-- Refresh token -->
                                    <div>
                                        <RadzenCheckBox Name="IsRefreshTokenGrantTypeEnabled" @bind-Value=updateClient.IsRefreshTokenGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.RefreshToken" Component="IsRefreshTokenGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                    <!-- Token exchange -->
                                    <div>
                                        <RadzenCheckBox Name="IsTokenExchangeEnabled" @bind-Value=updateClient.IsTokenExchangeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.TokenExchange" Component="IsTokenExchangeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                }

                                @if (Client.ClientType == ClientTypes.WEBSITE ||
                                   Client.ClientType == ClientTypes.MOBILE || 
                                   Client.ClientType == ClientTypes.EXTERNAL || 
                                   Client.ClientType == ClientTypes.HIGHLYSECUREDWEBSITE || 
                                   Client.ClientType == ClientTypes.GRANTMANAGEMENT)
                                {
                                    <!-- Authorization code  -->
                                    <div>
                                        <RadzenCheckBox Name="IsAuthorizationCodeGrantTypeEnabled" Disabled=true @bind-Value=updateClient.IsAuthorizationCodeGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.AuthorizationCode" Component="IsAuthorizationCodeGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                    <!-- CIBA -->
                                    <div>
                                        <RadzenCheckBox Name="IsCIBAGrantTypeEnabled" @bind-Value=updateClient.IsCIBAGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.CIBA" Component="IsCIBAGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                    <!-- UMA -->
                                    <div>
                                        <RadzenCheckBox Name="IsUMAGrantTypeEnabled" @bind-Value=updateClient.IsUMAGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.UMA" Component="IsUMAGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                    <!-- Refresh token -->
                                    <div>
                                        <RadzenCheckBox Name="IsRefreshTokenGrantTypeEnabled" @bind-Value=updateClient.IsRefreshTokenGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.RefreshToken" Component="IsRefreshTokenGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                    <!-- Client credentials  -->
                                    <div>
                                        <RadzenCheckBox Name="IsClientCredentialsGrantTypeEnabled" @bind-Value=updateClient.IsClientCredentialsGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.ClientCredentials" Component="IsClientCredentialsGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                }

                                @if (Client.ClientType == ClientTypes.DEVICE)
                                {
                                    <!-- DEVICE -->
                                    <div>
                                        <RadzenCheckBox Name="IsDeviceGrantTypeEnabled" @bind-Value=updateClient.IsDeviceGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.Device" Component="IsDeviceGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                }

                                @if(Client.ClientType == ClientTypes.CREDENTIALISSUER)
                                {                                    
                                    <!-- Authorization code  -->
                                    <div>
                                        <RadzenCheckBox Name="IsAuthorizationCodeGrantTypeEnabled" Disabled=true @bind-Value=updateClient.IsAuthorizationCodeGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.AuthorizationCode" Component="IsAuthorizationCodeGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                    <!-- Token exchange pre authorized code -->
                                    <div>
                                        <RadzenCheckBox Name="IsTokenExchangePreAuthorizedCodeEnabled" Disabled=true @bind-Value=updateClient.IsTokenExchangePreAuthorizedCodeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text=@Global.TokenExchangePreAuthorizedCode Component="IsTokenExchangePreAuthorizedCodeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                }

                                @if (Client.ClientType == ClientTypes.SPA)
                                {
                                    <!-- Authorization code  -->
                                    <div>
                                        <RadzenCheckBox Name="IsAuthorizationCodeGrantTypeEnabled" Disabled=true @bind-Value=updateClient.IsAuthorizationCodeGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.AuthorizationCode" Component="IsAuthorizationCodeGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                    <!-- Refresh token -->
                                    <div>
                                        <RadzenCheckBox Name="IsRefreshTokenGrantTypeEnabled" @bind-Value=updateClient.IsRefreshTokenGrantTypeEnabled></RadzenCheckBox>
                                        <RadzenLabel Text="@Global.RefreshToken" Component="IsRefreshTokenGrantTypeEnabled" Style="margin-left: 8px; vertical-align: middle;" />
                                    </div>
                                }
                            </div>
                        </div>
                    }

                    @if (Client.ClientType == ClientTypes.MACHINE)
                    {
                        <div class="row">
                            <div class="col-md-2">
                                <RadzenLabel Text="@Global.TokenExchangeType" Style="padding: 0px;" />
                            </div>
                            <div class="col">
                                <RadzenDropDown Name="IdTokenSignedResponseAlg" Class="w-100"
                                Data=@AllTokenExchangeTypes
                                TextProperty="Description"
                                ValueProperty="Value"
                                TValue="TokenExchangeTypes?"
                                @bind-Value=@updateClient.TokenExchangeType />
                            </div>
                        </div>
                    }

                    @if (Client.ClientType == ClientTypes.SAML)
                    {
                        <!-- Metadata Url -->
                        <div class="row">                            
                            <div class="col-md-2">
                                <RadzenLabel Text="@Global.MetadataUrl" Style="padding: 0px;" />
                            </div>
                            <div class="col">
                                <RadzenTextBox Name="MetadataUrl" @bind-Value="@updateClient.MetadataUrl" Class="w-100"></RadzenTextBox>
                            </div>
                        </div>
                        <!-- Use Artifact-->
                        <div class="row">
                            <div class="col-md-2">
                                <RadzenLabel Text="@Global.UseArtifact" Style="padding: 0px;" />
                            </div>
                            <div class="col">
                                <RadzenCheckBox Name="UseAcs" @bind-Value="@updateClient.UseAcs"></RadzenCheckBox>
                            </div>
                        </div>
                    }
                </RadzenAccordionItem>
            }

            @if (Client.ClientType == ClientTypes.SPA ||
               Client.ClientType == ClientTypes.WEBSITE || 
               Client.ClientType == ClientTypes.HIGHLYSECUREDWEBSITE || 
               Client.ClientType == ClientTypes.GRANTMANAGEMENT || 
               Client.ClientType == ClientTypes.MOBILE || 
               Client.ClientType == ClientTypes.EXTERNAL ||
               Client.ClientType == ClientTypes.CREDENTIALISSUER)
            {
                <RadzenAccordionItem Text="@Global.LoginSettings" Selected=true>
                    <!-- Consent required -->
                    <div class="row">
                        <div class="col-md-2">
                            <RadzenLabel Text="@Global.ConsentRequired" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenCheckBox @bind-Value=updateClient.IsConsentEnabled></RadzenCheckBox>
                        </div>
                    </div>
                    <!-- default acr methods -->
                    @if (Client.ClientType == ClientTypes.CREDENTIALISSUER ||
                      Client.ClientType == ClientTypes.SPA ||
                      Client.ClientType == ClientTypes.WEBSITE ||
                      Client.ClientType == ClientTypes.HIGHLYSECUREDWEBSITE)
                    {
                        <div class="row mt-2">
                            <div class="col-md-2">
                                <RadzenIcon Icon="info" MouseEnter="@(args => ShowTooltip(args, Global.DefaultClientAcrTooltip))" />
                                <RadzenLabel Text="@Global.DefaultClientAcr" Style="padding: 0px;" />
                            </div>
                            <div class="col">
                                <RadzenFormField Variant="Variant.Outlined">
                                    <ChildContent>
                                        <RadzenDropDown @bind-Value=@updateClient.DefaultAcrValues
                                        Chips=true
                                        Data=@AcrNames
                                        Multiple=true
                                        AllowClear=true
                                        class="w-100"></RadzenDropDown>
                                    </ChildContent>
                                    <Helper>
                                        @Global.ClientDefaultAcrValuesHelper
                                    </Helper>
                                </RadzenFormField>
                            </div>
                        </div>
                    }
                </RadzenAccordionItem>                
            }


            @if (Client.ClientType == ClientTypes.WEBSITE || 
               Client.ClientType == ClientTypes.HIGHLYSECUREDWEBSITE || 
               Client.ClientType == ClientTypes.GRANTMANAGEMENT || 
               Client.ClientType == ClientTypes.SPA || 
               Client.ClientType == ClientTypes.MOBILE || 
               Client.ClientType == ClientTypes.EXTERNAL ||
               Client.ClientType == ClientTypes.CREDENTIALISSUER)
            {
                <RadzenAccordionItem Text="Logout settings" Selected=true>
                    <!-- Front-channel logout session required -->
                    <div class="row">
                        <div class="col-md-2">
                            <RadzenIcon Icon="info" MouseEnter="@(args => ShowTooltip(args, Global.FrontChannelLogoutSessionRequiredTooltip))" />
                            <RadzenLabel Text="@Global.FrontChannelLogoutSessionRequired" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenCheckBox Name="FrontChannelLogoutSessionRequired" @bind-Value=updateClient.FrontChannelLogoutSessionRequired></RadzenCheckBox>
                        </div>
                    </div>
                    <!-- Front-channel logout URL -->
                    <div class="row mt-2">
                        <div class="col-md-2">
                            <RadzenIcon Icon="info" MouseEnter="@(args => ShowTooltip(args, Global.FrontChannelLogoutUriTooltip))" />
                            <RadzenLabel Text="@Global.FrontChannelLogoutUri" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenTextBox Name="FrontChannelLogoutUri" @bind-Value="@updateClient.FrontChannelLogoutUri" Class="w-100"></RadzenTextBox>
                        </div>
                    </div>
                    <!-- Backchannel logout session required  -->
                    <div class="row mt-2">
                        <div class="col-md-2">
                            <RadzenIcon Icon="info" MouseEnter="@(args => ShowTooltip(args, Global.BackChannelLogoutSessionRequiredTooltip))" />
                            <RadzenLabel Text="@Global.BackChannelLogoutSessionRequired" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenCheckBox Name="BackChannelLogoutSessionRequired" @bind-Value="@updateClient.BackChannelLogoutSessionRequired"></RadzenCheckBox>
                        </div>
                    </div>
                    <!-- Back-channel logout URL -->
                    <div class="row mt-2">
                        <div class="col-md-2">
                            <RadzenIcon Icon="info" MouseEnter="@(args => ShowTooltip(args, Global.BackChannelLogoutUriTooltip))" />
                            <RadzenLabel Text="@Global.BackChannelLogoutUri" Style="padding: 0px;" />
                        </div>
                        <div class="col">
                            <RadzenTextBox Name="BackChannelLogoutUri" @bind-Value="@updateClient.BackChannelLogoutUri" Class="w-100"></RadzenTextBox>
                        </div>
                    </div>
                </RadzenAccordionItem>
            }
        </Items>
    </RadzenAccordion>

    <SidAuthorizeView Roles=@("/clients/manage")>
        <Authorized>
            <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@(updateClientState.Value.IsUpdating ? Global.Updating : Global.Update)" Disabled=@updateClientState.Value.IsUpdating />
        </Authorized>
    </SidAuthorizeView>    
</RadzenTemplateForm>


@code {
    List<string> AcrNames
    {
        get 
        {
            return acrsState.Value.Acrs == null ? new List<string>() : acrsState.Value.Acrs.Select(a => a.Value.Name).ToList();
        }
    }

    [Parameter]
    public SimpleIdServer.IdServer.Domains.Client Client { get; set; }

    List<TokenExchangeType> AllTokenExchangeTypes = new List<TokenExchangeType>
    {
        new TokenExchangeType { Value = TokenExchangeTypes.IMPERSONATION, Description = Global.Impersonation },
        new TokenExchangeType { Value = TokenExchangeTypes.DELEGATION, Description = Global.Delegation }
    };

    List<AccessTokenTypesDesc> AllAccessTokenTypes = new List<AccessTokenTypesDesc>
    {
        new AccessTokenTypesDesc { Description = Global.Jwt, Value = AccessTokenTypes.Jwt },
        new AccessTokenTypesDesc { Description = Global.Reference, Value = AccessTokenTypes.Reference }
    };

    record AccessTokenTypesDesc
    {
        public string Description {get; set;}
        public AccessTokenTypes Value { get; set; }
    }

    record TokenExchangeType
    {
        public string Description { get; set; }
        public TokenExchangeTypes Value { get; set;}
    }

    record UpdateClient
    {
        public string ClientId { get; set; } = null!;
        public string? ClientName { get; set; } = null;
        public string? RedirectionUrls { get; set; } = null;
        public string? PostLogoutRedirectUris { get; set; } = null;
        public bool FrontChannelLogoutSessionRequired { get; set; }
        public string? FrontChannelLogoutUri { get; set; } = null;
        public string? BackChannelLogoutUri { get; set; } = null;
        public bool BackChannelLogoutSessionRequired { get; set; }
        public bool IsClientCredentialsGrantTypeEnabled { get; set; }
        public bool IsPasswordGrantTypeEnabled { get; set; }
        public bool IsRefreshTokenGrantTypeEnabled { get; set; }
        public bool IsTokenExchangeEnabled { get; set; }
        public bool IsAuthorizationCodeGrantTypeEnabled { get; set; }
        public bool IsTokenExchangePreAuthorizedCodeEnabled { get; set; }
        public bool IsCIBAGrantTypeEnabled { get; set; }
        public bool IsUMAGrantTypeEnabled { get; set; }
        public bool IsConsentEnabled { get; set; }
        public bool IsDeviceGrantTypeEnabled { get; set; }
        public bool UseAcs { get; set; }
        public bool IsRedirectUrlCaseSensitive { get; set; }
        public bool RedirectToRevokeSessionUI { get; set; }
        public string MetadataUrl { get; set; }
        public string ConcatenatedClientRegistrationTypes { get; set; }
        public ICollection<string> DefaultAcrValues { get; set; }
        public TokenExchangeTypes? TokenExchangeType { get; set;  }
        public AccessTokenTypes AccessTokenType { get; set;  }
        public DateTime? ExpirationDateTime { get; set; } = null;
        public bool IsPublic { get; set; }
    }

    UpdateClient updateClient = new UpdateClient();

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            SubscribeToAction<UpdateClientDetailsSuccessAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.ClientSettingsUpdated });
                StateHasChanged();
            });
            SubscribeToAction<UpdateClientDetailsFailureAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
                StateHasChanged();
            });
        }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (this.Client == null) return;
        updateClient = new UpdateClient
        {
            ClientId = this.Client.ClientId,
            ClientName = this.Client.ClientName,
            RedirectionUrls = string.Join(';', this.Client.RedirectionUrls),
            PostLogoutRedirectUris = string.Join(';', this.Client.PostLogoutRedirectUris),
            FrontChannelLogoutUri = this.Client.FrontChannelLogoutUri,
            BackChannelLogoutUri = this.Client.BackChannelLogoutUri,
            FrontChannelLogoutSessionRequired = this.Client.FrontChannelLogoutSessionRequired,
            BackChannelLogoutSessionRequired = this.Client.BackChannelLogoutSessionRequired,
            IsConsentEnabled = !this.Client.IsConsentDisabled,
            UseAcs = this.Client.GetUseAcrsArtifact(),
            MetadataUrl = this.Client.GetSaml2SpMetadataUrl(),
            IsRedirectUrlCaseSensitive = this.Client.IsRedirectUrlCaseSensitive,
            RedirectToRevokeSessionUI = this.Client.RedirectToRevokeSessionUI,
            TokenExchangeType = this.Client.TokenExchangeType,
            AccessTokenType = this.Client.AccessTokenType,
            ConcatenatedClientRegistrationTypes = this.Client.ClientRegistrationTypesSupported == null ? string.Empty : string.Join(";", this.Client.ClientRegistrationTypesSupported),
            DefaultAcrValues = this.Client.DefaultAcrValues.Where(a => !string.IsNullOrWhiteSpace(a)).ToList(),
            ExpirationDateTime = this.Client.ExpirationDateTime,
            IsPublic = this.Client.IsPublic
        };
        if (this.Client.GrantTypes.Contains(ClientCredentialsHandler.GRANT_TYPE))
            updateClient.IsClientCredentialsGrantTypeEnabled = true;
        if (this.Client.GrantTypes.Contains(PasswordHandler.GRANT_TYPE))
            updateClient.IsPasswordGrantTypeEnabled = true;
        if (this.Client.GrantTypes.Contains(RefreshTokenHandler.GRANT_TYPE))
            updateClient.IsRefreshTokenGrantTypeEnabled = true;
        if (this.Client.GrantTypes.Contains(AuthorizationCodeHandler.GRANT_TYPE))
            updateClient.IsAuthorizationCodeGrantTypeEnabled = true;
        if (this.Client.GrantTypes.Contains(TokenExchangePreAuthorizedCodeHandler.GRANT_TYPE))
            updateClient.IsTokenExchangePreAuthorizedCodeEnabled = true;
        if (this.Client.GrantTypes.Contains(CIBAHandler.GRANT_TYPE))
            updateClient.IsCIBAGrantTypeEnabled = true;
        if (this.Client.GrantTypes.Contains(UmaTicketHandler.GRANT_TYPE))
            updateClient.IsUMAGrantTypeEnabled = true;
        if (this.Client.GrantTypes.Contains(DeviceCodeHandler.GRANT_TYPE))
            updateClient.IsDeviceGrantTypeEnabled = true;
        if (this.Client.GrantTypes.Contains(TokenExchangeHandler.GRANT_TYPE))
            updateClient.IsTokenExchangeEnabled = true;
    }

    void SubmitUpdateClient(UpdateClient client)
    {
        dispatcher.Dispatch(new UpdateClientDetailsAction 
        { 
            Id = this.Client.Id,
            ClientId = client.ClientId,
            ClientName = client.ClientName,
            RedirectionUrls = client.RedirectionUrls,
            PostLogoutRedirectUris = client.PostLogoutRedirectUris,
            FrontChannelLogoutSessionRequired = client.FrontChannelLogoutSessionRequired,
            FrontChannelLogoutUri = client.FrontChannelLogoutUri,
            BackChannelLogoutUri = client.BackChannelLogoutUri,
            BackChannelLogoutSessionRequired = client.BackChannelLogoutSessionRequired,
            IsClientCredentialsGrantTypeEnabled = client.IsClientCredentialsGrantTypeEnabled,
            IsPasswordGrantTypeEnabled = client.IsPasswordGrantTypeEnabled,
            IsRefreshTokenGrantTypeEnabled = client.IsRefreshTokenGrantTypeEnabled,
            IsAuthorizationCodeGrantTypeEnabled = client.IsAuthorizationCodeGrantTypeEnabled,
            IsCIBAGrantTypeEnabled = client.IsCIBAGrantTypeEnabled,
            IsUMAGrantTypeEnabled = client.IsUMAGrantTypeEnabled,
            IsConsentEnabled = client.IsConsentEnabled,
            IsDeviceGrantTypeEnabled = client.IsDeviceGrantTypeEnabled,
            IsTokenExchangeEnabled = client.IsTokenExchangeEnabled,
            IsRedirectUrlCaseSensitive = client.IsRedirectUrlCaseSensitive,
            UseAcs = client.UseAcs,
            MetadataUrl = client.MetadataUrl,
            TokenExchangeType = client.TokenExchangeType,
            JwksUrl = this.Client.JwksUri,
            AccessTokenType = client.AccessTokenType,
            RedirectToRevokeSessionUI = client.RedirectToRevokeSessionUI,
            IsTokenExchangePreAuthorizedCodeEnabled = client.IsTokenExchangePreAuthorizedCodeEnabled,
            DefaultAcrValues = client.DefaultAcrValues,
            IsPublic = client.IsPublic
        });
    }

    void ShowTooltip(ElementReference eltRef, string msg)
    {
        tooltipService.Open(eltRef, msg, null);
    }
}